package flc.ast.adapter;

import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.text.SimpleDateFormat;

import flc.ast.R;
import flc.ast.bean.MyFileBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class MyFileAdapter extends StkProviderMultiAdapter<MyFileBean> {

    public MyFileAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(80));
        addItemProvider(new MyFileProvider());
    }

    private class MyFileProvider extends BaseItemProvider<MyFileBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_my_file;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyFileBean bean) {
            baseViewHolder.setImageResource(R.id.ivMyFileItemImg, getImgIcon(bean.getPath()));
            baseViewHolder.setText(R.id.tvMyFileItemName, FileUtils.getFileName(bean.getPath()));
            baseViewHolder.setText(R.id.tvMyFileItemDate, TimeUtils.millis2String(FileUtils.getFileLastModified(bean.getPath()),new SimpleDateFormat("yyyy-MM-dd")));
            baseViewHolder.setText(R.id.tvMyFileItemSize, FileUtils.getSize(bean.getPath()));
            if (bean.isSelected()) {
                baseViewHolder.setImageResource(R.id.ivMyFileItemSel, R.drawable.axzjmwj);
            } else {
                baseViewHolder.setImageResource(R.id.ivMyFileItemSel, R.drawable.awxz3);
            }
        }
    }

    //根据后缀获取图标
    private int getImgIcon(String path) {
        String suffix = FileUtils.getFileExtension(path);
        int icon;
        if (suffix.equalsIgnoreCase("pdf")) {
            icon = R.drawable.apdf3;
        } else if (suffix.equalsIgnoreCase("ppt") || suffix.equalsIgnoreCase("pptx")) {
            icon = R.drawable.appt;
        } else if (suffix.equalsIgnoreCase("txt")) {
            icon = R.drawable.atxt;
        } else if (suffix.equalsIgnoreCase("doc") || suffix.equalsIgnoreCase("docx")) {
            icon = R.drawable.adoc;
        } else if (suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx")) {
            icon = R.drawable.axls;
        } else if (suffix.equalsIgnoreCase("rar") || suffix.equalsIgnoreCase("zip")) {
            icon = R.drawable.ayasuobao;
        } else {
            icon = R.drawable.awenjianbao;
        }
        return icon;
    }
}
